#include "WPolygons.h"

WPolygons::WPolygons()
{
    //ctor
    _isOk=false ;
    _waterSurfaceXSize = 0 ;
    _waterSurfaceYSize = 0 ;
}

WPolygons::~WPolygons()
{
    //dtor
}

bool WPolygons::initFromFile(string hdfFilename )
{
    cout<<"WPolygons::initFromFile "<<hdfFilename<<endl ;
    _isOk = false ;
    {

        string dsname1 = "HDF5:" + hdfFilename + "://Geometry/2D_Flow_Areas/1/Cells_Center_Coordinate" ;
        GDALDatasetH ds = (GDALDatasetH)GDALOpen( dsname1.c_str() , GA_ReadOnly) ;
        if( ds == 0 ){
            cout<<"bad dsname "<<dsname1<<endl ;
            return false ;
        }
        int xsize = GDALGetRasterXSize(ds) ;
        int ysize = GDALGetRasterYSize(ds) ;
        _cellsCenterCoorVec.resize(xsize*ysize) ;
        GDALRasterBandH band1=GDALGetRasterBand(ds,1) ;
        GDALRasterIO(band1,GF_Read,0,0,xsize,ysize, _cellsCenterCoorVec.data() , xsize, ysize, GDT_Float64 , 0,0 ) ;
        GDALClose(ds) ;
    }

    {

        string dsname1 = "HDF5:" + hdfFilename + "://Geometry/2D_Flow_Areas/1/FacePoints_Coordinate" ;
        GDALDatasetH ds = (GDALDatasetH)GDALOpen( dsname1.c_str() , GA_ReadOnly) ;
        if( ds == 0 ){
            cout<<"bad dsname "<<dsname1<<endl ;
            return false ;
        }
        int xsize = GDALGetRasterXSize(ds) ;
        int ysize = GDALGetRasterYSize(ds) ;
        _facePointsCoorVec.resize(xsize*ysize) ;
        GDALRasterBandH band1=GDALGetRasterBand(ds,1) ;
        GDALRasterIO(band1,GF_Read,0,0,xsize,ysize, _facePointsCoorVec.data() , xsize, ysize, GDT_Float64 , 0,0 ) ;
        GDALClose(ds) ;
    }

    {

        string dsname1 = "HDF5:" + hdfFilename + "://Geometry/2D_Flow_Areas/1/Cells_FacePoint_Indexes" ;
        GDALDatasetH ds = (GDALDatasetH)GDALOpen( dsname1.c_str() , GA_ReadOnly) ;
        if( ds == 0 ){
            cout<<"bad dsname "<<dsname1<<endl ;
            return false ;
        }
        int xsize = GDALGetRasterXSize(ds) ;
        int ysize = GDALGetRasterYSize(ds) ;
        _cellsFacePointsIndexVec.resize(xsize*ysize) ;
        GDALRasterBandH band1=GDALGetRasterBand(ds,1) ;
        GDALRasterIO(band1,GF_Read,0,0,xsize,ysize, _cellsFacePointsIndexVec.data() , xsize, ysize, GDT_Int32 , 0,0 ) ;
        GDALClose(ds) ;
    }

    {

        string dsname1 = "HDF5:" + hdfFilename + "://Results/Unsteady/Output/Output_Blocks/Base_Output/Unsteady_Time_Series/2D_Flow_Areas/1/Water_Surface" ;
        GDALDatasetH ds = (GDALDatasetH)GDALOpen( dsname1.c_str() , GA_ReadOnly) ;
        if( ds == 0 ){
            cout<<"bad dsname "<<dsname1<<endl ;
            return false ;
        }
        int xsize = GDALGetRasterXSize(ds) ;
        int ysize = GDALGetRasterYSize(ds) ;
        _waterSurface.resize(xsize*ysize) ;
        _waterSurfaceXSize = xsize;
        _waterSurfaceYSize = ysize;
        GDALRasterBandH band1=GDALGetRasterBand(ds,1) ;
        GDALRasterIO(band1,GF_Read,0,0,xsize,ysize, _waterSurface.data() , xsize, ysize, GDT_Float32 , 0,0 ) ;
        GDALClose(ds) ;
    }

    //time date stamp
    {
        File file(hdfFilename, File::ReadOnly);
        auto dataset = file.getDataSet("/Results/Unsteady/Output/Output Blocks/Base Output/Unsteady Time Series/Time Date Stamp");
        const int realSize = dataset.getElementCount() ;
        const int TIME_COUNT = 20000; //
        FixedLenStringArray<TIME_COUNT> strs;
        dataset.read( strs );
        _timeDateVec.resize(realSize) ;
        for(int it = 0; it <realSize;++it ){
            _timeDateVec[it] = strs.at(it) ;
        }
    }

    cout<<"WPolygons::initFromFile done"<<endl ;
    _isOk=true;
    return _isOk ;
}


bool WPolygons::buildCellLines( vector<CellLines>& ret_cells )
{
    if( this->isOk()==false ) {
        cout<<"WPolygons::buildCellLines failed, isOK is false."<<endl ;
        return false;
    }
    int numOfCells = _cellsFacePointsIndexVec.size() / 5 ;
    for(int icell = 0 ; icell < numOfCells ; ++ icell )
    {
        int goodVert = 0 ;
        for(int iv = 0 ; iv < 5 ; ++iv ){
            if( _cellsFacePointsIndexVec[icell*5+iv] >=0 ){
                ++goodVert;
            }
        }
        // a good cell should have at least 3 vertices.
        if( goodVert>=3 )
        {
            CellLines cell ;
            cell.cellindex = icell ;
            for(int iv = 0 ; iv < goodVert; ++ iv )
            {
                int index0 = _cellsFacePointsIndexVec[icell*5+iv] ;
                int index1 = _cellsFacePointsIndexVec[icell*5+iv+1] ;
                if( iv==goodVert-1 ) index1 = _cellsFacePointsIndexVec[icell*5+0] ;//last vertex then go circle to first vertex.
                double pt0x = _facePointsCoorVec[index0*2+0] ;
                double pt0y = _facePointsCoorVec[index0*2+1] ;
                double pt1x = _facePointsCoorVec[index1*2+0] ;
                double pt1y = _facePointsCoorVec[index1*2+1] ;
                WShpLineSeg lineseg(pt0x,pt0y, pt1x, pt1y) ;
                cell.linesegments.push_back(lineseg) ;
            }
            ret_cells.push_back(cell) ;
        }
    }
    return true;
}
